import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
    routes: [
  		{ path: '/', redirect: '/home' }, // 默认跳转页面
	    {
	      path: '/home',
	      name: 'home',
	      component: resolve => require(['@/components/home'], resolve),
	      redirect: '/home/index',
	      children:[
	      	{
		      path: '/home/index',
		      name: 'index',
		      component: resolve => require(['@/components/index'], resolve),
	     	  meta:{title:'赶紧试'}
		    },
		    {
		      path: '/home/good',
		      name: 'good',
		      component: resolve => require(['@/components/good'], resolve),
	     	  meta:{title:'赶紧试'}
		    },     
	      	{
		      path: '/home/myActivity',
		      name: 'myActivity',
		      component: resolve => require(['@/components/myActivity'], resolve),
		      meta:{
		      	title:'我的试用',
		      	requireAuth: true,
		      }
		    },
	      	{
		      path: '/home/myAuction',
		      name: 'myAuction',
		      component: resolve => require(['@/components/myAuction'], resolve),
		      meta:{
		      	title:'我的拍卖',
		      	requireAuth: true,
		      }
		    },
		    {
		      path: '/home/my',
		      name: 'my',
		      component: resolve => require(['@/components/my'], resolve),
		      meta:{
		      	title:'我的',
		      	requireAuth: true,
		      }
		    },
	      ]
	    },
	    {
		    path: '/taskDetail/:taskId',
		    name: 'taskDetail',
		    component:  resolve => require(['@/components/taskDetail'], resolve),
	     	meta:{title:'试用详情'}
		},
	    {
		    path: '/taskDetailAuction/:taskId',
		    name: 'taskDetailAuction',
		    component:  resolve => require(['@/components/taskDetailAuction'], resolve),
	     	meta:{title:'拍卖详情'}
		},
      	{
      		path: '/user',
		    name: 'user',
		    component: resolve => require(['@/components/user'], resolve),
		    meta:{
		    	title:'用户',
		      	requireAuth: true,
		    },
		    children:[
		    	{
		      		path: '/user/taskDetail1',
				    name: 'taskDetail1',
				    component: resolve => require(['@/components/taskDetail1'], resolve),
				    meta:{title:'试用详情'}
		      	},
		    	{
		      		path: '/user/process/:taskId',
				    name: 'process',
				    component: resolve => require(['@/components/process'], resolve),
				    meta:{title:'试用流程'}
		      	},
		    	{
		      		path: '/user/processAuction/:taskId',
				    name: 'processAuction',
				    component: resolve => require(['@/components/processAuction'], resolve),
				    meta:{title:'拍卖流程'}
		      	},
		    	{
		      		path: '/user/recharge',
				    name: 'recharge',
				    component: resolve => require(['@/components/recharge'], resolve),
				    meta:{title:'金币充值'}
		      	},
		    	{
		      		path: '/user/mygold',
				    name: 'mygold',
				    component: resolve => require(['@/components/mygold'], resolve),
				    meta:{title:'我的金币'}
		        },
		    	{
		      		path: '/user/makegold',
				    name: 'makegold',
				    component: resolve => require(['@/components/makegold'], resolve),
				    meta:{title:'赚金币'}
		      	},
		    	{
		      		path: '/user/luckyDraw',
				    name: 'luckyDraw',
				    component: resolve => require(['@/components/luckyDraw'], resolve),
				    meta:{title:'金币抽奖'}
		      	},
		    	{
		      		path: '/user/balance',
				    name: 'balance',
				    component: resolve => require(['@/components/balance'], resolve),
				    meta:{title:'账户余额'}
		      	},
		    	{
		      		path: '/user/withdrawal',
				    name: 'withdrawal',
				    component: resolve => require(['@/components/withdrawal'], resolve),
				    meta:{title:'提现'}
		      	},
		    	{
		      		path: '/user/withdrawalList',
				    name: 'withdrawalList',
				    component: resolve => require(['@/components/withdrawalList'], resolve),
				    meta:{title:'提现列表'}
		      	},
		    	{
		      		path: '/user/stream',
				    name: 'stream',
				    component: resolve => require(['@/components/stream'], resolve),
				    meta:{title:'账户明细'}
		      	},
		    	{
		      		path: '/user/WinningRate',
				    name: 'WinningRate',
				    component: resolve => require(['@/components/WinningRate'], resolve),
				    meta:{title:'我的中奖率PK'}
		      	},
		    	{
		      		path: '/user/help',
				    name: 'help',
				    component: resolve => require(['@/components/help'], resolve),
				    meta:{title:'客服与帮助'}
		      	},
		    	{
		      		path: '/user/helpProcess',
				    name: 'helpProcess',
				    component: resolve => require(['@/components/helpProcess'], resolve),
				    meta:{title:'帮助中心'}
		      	},
		    	{
		      		path: '/user/service',
				    name: 'service',
				    component: resolve => require(['@/components/service'], resolve),
				    meta:{title:'联系客服'}
		      	},
		    	{
		      		path: '/user/account',
				    name: 'account',
				    component: resolve => require(['@/components/account'], resolve),
				    meta:{title:'账号安全'}
		      	},
		    	{
		      		path: '/user/phone',
				    name: 'phone',
				    component: resolve => require(['@/components/phone'], resolve),
				    meta:{title:'修改手机号'}
		      	},
		    	{
		      		path: '/user/bankcard',
				    name: 'bankcard',
				    component: resolve => require(['@/components/bankcard'], resolve),
				    meta:{title:'绑定银行卡'}
		      	},
		    	{
		      		path: '/user/accountTB',
				    name: 'accountTB',
				    component: resolve => require(['@/components/accountTB'], resolve),
				    meta:{title:'绑定淘宝账号'}
		      	},
		    	{
		      		path: '/user/accountJD',
				    name: 'accountJD',
				    component: resolve => require(['@/components/accountJD'], resolve),
				    meta:{title:'绑定京东账号'}
		      	},
		    	{
		      		path: '/user/accountQQ',
				    name: 'accountQQ',
				    component: resolve => require(['@/components/accountQQ'], resolve),
				    meta:{title:'绑定QQ账号'}
		      	},
		    	{
		      		path: '/user/password',
				    name: 'password',
				    component: resolve => require(['@/components/password'], resolve),
				    meta:{title:'修改密码'}
		      	},
		    	{
		      		path: '/user/findPayPw',
				    name: 'findPayPw',
				    component: resolve => require(['@/components/findPayPw'], resolve),
				    meta:{title:'找回支付密码'}
		      	},
		    	{
		      		path: '/user/vip',
				    name: 'vip',
				    component: resolve => require(['@/components/vip'], resolve),
				    meta:{title:'VIP会员'}
		      	},
		    	{
		      		path: '/user/feedback',
				    name: 'feedback',
				    component: resolve => require(['@/components/feedback'], resolve),
				    meta:{title:'意见反馈'}
		      	},
		    	{
		      		path: '/user/news',
				    name: 'news',
				    component: resolve => require(['@/components/news'], resolve),
				    meta:{title:'消息'}
		      	},
		    	{
		      		path: '/user/shareUser',
				    name: 'shareUser',
				    component: resolve => require(['@/components/shareUser'], resolve),
				    meta:{title:'代言人'}
		      	},
		    	{
		      		path: '/user/spokesman',
				    name: 'spokesman',
				    component: resolve => require(['@/components/spokesman'], resolve),
				    meta:{title:'什么是代言人'}
		      	},
		    	{
		      		path: '/user/minivan',
				    name: 'minivan',
				    component: resolve => require(['@/components/minivan'], resolve),
				    meta:{title:'什么是微客'}
		      	},
		    	{
		      		path: '/user/shareAgent',
				    name: 'shareAgent',
				    component: resolve => require(['@/components/shareAgent'], resolve),
				    meta:{title:'微客'}
		      	},
		    	{
		      		path: '/user/myFans',
				    name: 'myFans',
				    component: resolve => require(['@/components/myFans'], resolve),
				    meta:{title:'我的粉丝'}
		      	},
		    	{
		      		path: '/user/myAgent',
				    name: 'myAgent',
				    component: resolve => require(['@/components/myAgent'], resolve),
				    meta:{title:'我的商家'}
		      	},
		    ]
      	},
	    {
	      path: '/share',
	      name: 'share',
	      component: resolve => require(['@/components/share'], resolve),
	      meta:{title:'赶紧试'}
	    },
	    {
	      path: '/login',
	      name: 'login',
	      component: resolve => require(['@/components/login'], resolve),
	      meta:{title:'登录'}
	    },
	    {
	      path: '/register',
	      name: 'register',
	      component: resolve => require(['@/components/register'], resolve),
	      meta:{title:'注册'}
	    },
	    {
	      path: '/registerAgent',
	      name: 'registerAgent',
	      component: resolve => require(['@/components/registerAgent'], resolve),
	      meta:{title:'商家注册'}
	    },
	    {
	      path: '/agreementAgent',
	      name: 'agreementAgent',
	      component: resolve => require(['@/components/agreementAgent'], resolve),
	      meta:{title:'用户协议'}
	    },
	    {
	      path: '/forget',
	      name: 'forget',
	      component: resolve => require(['@/components/forget'], resolve),
	      meta:{title:'忘记密码'}
	    },
	    {
	      path: '/share2',
	      name: 'share2',
	      component: resolve => require(['@/components/share2'], resolve),
	      meta:{title:'赶紧试'}
	    },
	    {
	      path: '/agreement',
	      name: 'agreement',
	      component: resolve => require(['@/components/agreement'], resolve),
	      meta:{title:'用户规则'}
	    },
	    {
	      path: '/exchange',
	      name: 'exchange',
	      component: resolve => require(['@/components/exchange'], resolve),
	      meta:{title:'金币兑换'}
	    },
	    {
	      path: '/secKill',
	      name: 'secKill',
	      component: resolve => require(['@/components/secKill'], resolve),
	      meta:{title:'限时秒杀'}
	    },
	    {
	      path: '/auction',
	      name: 'auction',
	      component: resolve => require(['@/components/auction'], resolve),
	      meta:{title:'赶紧拍'}
	    },
    ],
    scrollBehavior (to, from, savedPosition) {
	    if (savedPosition) {
	      return savedPosition
	    } else {
	      return { x: 0, y: 0 }
	    }
	}
})

router.beforeEach((to, from, next) => {
    if (to.matched.some(r => r.meta.requireAuth)) {
    	var usertoken = localStorage.getItem('usertoken');
        if (usertoken) {
            next();
        }
        else {
            next({
                path: '/login',
                query: {redirect: to.path}
            })
        }
    }
    else {
        next();
    }
})
export default router;

